import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex);
export const store = new Vuex.Store({
    plugins: [createPersistedState()],
    state:{
        product:{
            "count": 1
        },
        login:false,
        uid:'-1',
        cartNum:0,
        searchList:[]
    },
    getters:{
        login:state=>{
            return state.login
        },
        uid:state=>{
            return state.uid
        },
        cartNum:state=>{
            return state.cartNum
        },
        count:state=>{
            return state.product.count
        },
        searchList:state=>{
            return state.searchList
        }
    },
    mutations:{
        increment:(state)=>{
            if(state.product.count<99){
                state.product.count++;
            }else{
                state.product.count=99
            }
        },
        decrement:(state)=>{
            if(state.product.count>1){
                state.product.count--;
            }else{
                state.product.count=1
            }
        },
        resetCount:(state,newCount)=>{
            state.product.count=newCount
        },
        checkLogin:(state,payload)=>{
            state.login = payload
        },
        setUid:(state,payload)=>{
            state.uid = payload
        },
        setcartNum:(state,payload)=>{
            state.cartNum=payload;
        },
        increase:(state,payload)=>{
            if(state.cartNum<99){
                state.cartNum+=payload;
            }else{
                state.cartNum=99
            }
        },
        decrease:(state,payload)=>{
            if(state.cartNum>1){
                state.cartNum-=payload;
            }else{
                state.cartNum=1
            }
        },
        changeSearch:(state,newValue)=>{
            state.searchList=newValue
        }
    },
    actions:{
        //增加cartNum
        increase:({commit},payload)=>{
            commit('increase',payload);
        },
        //减少cartNum
        decrease:({commit},payload)=>{
            commit('decrease',payload);
        },
        checkLogin:({commit},payload)=>{
            commit('checkLogin',payload);
        },
        setUid:({commit},payload)=>{
            commit('setUid',payload);
        },
        setcartNum:({commit},payload)=>{
            commit('setcartNum',payload);
        },
        changeSearch:({commit},payload)=>{
            commit('changeSearch',payload);
        }
    }
});